package com.kidedu.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.ibatis.session.RowBounds;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.kidedu.dao.mapper.KTeacherMapper;
import com.kidedu.entity.KTeacher;
import com.kidedu.entity.KTeacherExample;
import com.kidedu.service.IKTeacherService;
import com.kidedu.util.BaseService;
import com.opensymphony.xwork2.ActionContext;

public class KTeacherService extends BaseService implements IKTeacherService{
	
	@Autowired
	private KTeacherMapper kTeacherMapper;
	
	public List<KTeacher> selectKTeacherList() {
		// TODO Auto-generated method stub
		KTeacherExample teacherExample=new KTeacherExample();
		return kTeacherMapper.selectByExample(teacherExample);
	}
	
	public JSONObject BatchImport(Sheet sheet) {
		// TODO Auto-generated method stub
		JSONObject json=new JSONObject();
		int total=sheet.getLastRowNum();
		 Row row=null;
		 KTeacher teacher;
		 for(int i=0;i<=total;i++)
			{
				int j=-1;
				row=sheet.getRow(i);
				teacher=new KTeacher();
				row=sheet.getRow(i);
			
				 try{
					 row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
					 teacher.setTusername(row.getCell(0).getRichStringCellValue().toString());
					 teacher.setTpassword("123456");
					 row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
				     BigDecimal  bd = new BigDecimal(row.getCell(1).getRichStringCellValue().getString());  
					 teacher.setTtelnumber(bd.toPlainString());
					 row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
					 String Level=row.getCell(2).getRichStringCellValue().toString();
					// int convertToInteger=(int)Double.parseDouble(Level);			 	
					// teacher.setTlevel(""+convertToInteger);	 
					 teacher.setTlevel(Byte.valueOf(Level));
			           j= kTeacherMapper.insertSelective(teacher);
				   if(j==-1)
				   {	
					   json.put("error", "");
				   }
				   }
				 catch (Exception e)
				 {
					   json.put("error2", "");
				 }
			
			}
		 json.put("message", "");
		return null;
	}
public JSONObject TeacherInfoFY(int pageSize, int PageNo) {
	// TODO Auto-generated method stub
	 JSONObject json=new JSONObject();
	KTeacherExample teacherExample=new KTeacherExample();
	List<KTeacher> teacherList=kTeacherMapper.selectByExample(teacherExample,  new RowBounds(super.calculateStartPage(PageNo, pageSize), pageSize));
	int count =kTeacherMapper.countByExample(teacherExample);
	json.put("TeacherList", teacherList);
	json.put("totel", count);
	return json;
}

public boolean Login(String username, String password) {
	// TODO Auto-generated method stub
	KTeacherExample teacherExample=new KTeacherExample();
	teacherExample.createCriteria().andTusernameEqualTo(username).andTpasswordEqualTo(password);
	KTeacher teacher=(KTeacher) kTeacherMapper.selectByExample(teacherExample).get(0);
	if(null!=teacher)
	{
		HttpServletRequest req = ServletActionContext.getRequest();
		req.getSession().setAttribute("TEACHER", teacher);
		
	    ActionContext.getContext().getSession().put("T_id", teacher.getTid());
		return true;
	}
	return false;
}

@Override
public boolean updateTeacher(KTeacher teacher) {
	// TODO Auto-generated method stub
	kTeacherMapper.updateByPrimaryKeySelective(teacher);
	return true;
}

@Override
public boolean addSingleTeacher(KTeacher teacher) {
	// TODO Auto-generated method stub
	kTeacherMapper.insertSelective(teacher);
	return true;
}

@Override
public boolean deleteTeacher(String t_id) {
	// TODO Auto-generated method stub
	kTeacherMapper.deleteByPrimaryKey(t_id);
	return true;
}
	
}